<?php

class PublicAction extends Action
{

    public $loginMarked;

    /**
     * +----------------------------------------------------------
     * 初始化
     * +----------------------------------------------------------
     */
    public function _initialize()
    {
        header("Content-Type:text/html; charset=utf-8");
        //  header('Content-Type:application/json; charset=utf-8');
        $loginMarked = C("TOKEN");
        $this->loginMarked = md5($loginMarked['admin_marked']);
    }


    public function index()
    {

        if (IS_POST)
        {
            $returnLoginInfo = D("Public")->auth();

       /*     $this->show('<script type="text/javascript" src="__PUBLIC__/libs/js/jquery.js"></script>
<script type="text/javascript" src="__PUBLIC__/libs/js/popup/drag.js"></script>
<script type="text/javascript" src="__PUBLIC__/libs/js/popup/dialog.js"></script>
<link href="__PUBLIC__/libs/skins/blue/style.css" rel="stylesheet" type="text/css" id="theme" themeColor="blue"/>
	   <script type="text/javascript">
	   $(function(){
	   top.Dialog.open({URL:"__APP__/Appointment/index/",Title:"预约信息 ",Width:600,Height:480});
	   })</script>
	   ', 'utf-8');*/

            $this->redirect($returnLoginInfo["url"]);

        } else
        {
            if (isset($_COOKIE[$this->loginMarked]))
            {
                $this->redirect("Index/index");
            }

            $systemConfig = include WEB_ROOT . 'Common/systemConfig.php';
            $this->assign("site", $systemConfig);
            $this->display("Common:login");
        }
    }

    public function loginOut()
    {
        setcookie("$this->loginMarked", NULL, -3600, "/");
        unset($_SESSION["$this->loginMarked"], $_COOKIE["$this->loginMarked"]);
        if (isset($_SESSION[C('USER_AUTH_KEY')]))
        {
            unset($_SESSION[C('USER_AUTH_KEY')]);
            unset($_SESSION);
            session_destroy();
        }
        $this->redirect("Index/index");
    }

    public function findPwd()
    {
        $M = M("Admin");
        if (IS_POST)
        {
            $this->checkToken();
            echo json_encode(D("Public")->findPwd());
        } else
        {
            setcookie("$this->loginMarked", NULL, -3600, "/");
            unset($_SESSION["$this->loginMarked"], $_COOKIE["$this->loginMarked"]);
            $cookie = $this->_get('code');
            $shell = substr($cookie, -32);
            $aid = (int)str_replace($shell, '', $cookie);
            $info = $M->where("`aid`='$aid'")->find();
            if ($info['status'] == 0)
            {
                $this->error("你的账号被禁用，有疑问联系管理员吧", __APP__);
            }
            if (md5($info['find_code']) == $shell)
            {
                $this->assign("info", $info);
                $_SESSION['aid'] = $aid;
                $systemConfig = include WEB_ROOT . 'Common/systemConfig.php';
                $this->assign("site", $systemConfig);
                $this->display("Common:findPwd");
            } else
            {
                $this->error("验证地址不存在或已失效", __APP__);
            }
        }
    }

}